import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    testPaperInfo: {},
    // 用户入学测试信息
    entranceExam: null,
    userInfo: {},
    courseOrPaperInfo: {
      name: '',
      courseId: '',
      paperId: '',
      examId: ''
    },
    errorQuestionInfo: {},
    entranceExamFlag: null, // 是否进行过入学测试考试
    videoUrl: '',
    userErrorQuestionList: [],
   // host: 'http://180.76.146.67', // 线上api接口地址
  //  fileHost: 'http://180.76.146.67/uploads',
   //  host: 'http://education.gxsrkj.com', // 线上api接口地址
   // fileHost: 'http://education.gxsrkj.com/uploads',

    host: 'http://localhost',
    fileHost: 'http://localhost/uploads',
    proxyHttp: true // 是否开启http代理
  },

  mutations: {
    updateUserErrorQuestionList (state, data) {
      state.userErrorQuestionList = data
      sessionStorage.setItem('userErrorQuestionList', JSON.stringify(data))
    },

    updateErrorQuestionInfo (state, data) {
      state.errorQuestionInfo = data
    },

    updateCourseOrPaperInfo (state, data) {
      state.courseOrPaperInfo = data
      sessionStorage.setItem('courseOrPaperInfo', JSON.stringify(data))
    },

    updateTestPaperInfo (state, data) {
      state.testPaperInfo = data
      sessionStorage.setItem('testPaperInfo', JSON.stringify(data))
    },

    updateEntranceExamFlag (state, data) {
      state.entranceExamFlag = data
      sessionStorage.setItem('entranceExamFlag', data)
    },

    updateUserInfo (state, data) {
      state.userInfo = data
      localStorage.setItem('userInfo', JSON.stringify(data))
    },

    updateEntranceExam (state, data) {
      state.entranceExam = data
      localStorage.setItem('entranceExam', JSON.stringify(data))
    },

    updateVideoUrl (state, data) {
      state.videoUrl = data
      sessionStorage.setItem('videoUrl', JSON.stringify(data))
    }
  },

  getters: {

    getEntranceExam (state) {
      if (state.entranceExam) {
        return state.entranceExam
      }
      return JSON.parse(localStorage.getItem('entranceExam'))
    },

    getEntranceExamFlag (state) {
      if (state.entranceExamFlag) {
        return state.entranceExamFlag
      }
      return JSON.parse(sessionStorage.getItem('entranceExamFlag'))
    },

    getCourseOrPaperInfo (state) {
      if (state.courseOrPaperInfo.name) {
        return state.courseOrPaperInfo
      }
      return JSON.parse(sessionStorage.getItem('courseOrPaperInfo'))
    },

    getTestPaperInfo (state) {
      if (state.testPaperInfo.name) {
        return state.testPaperInfo
      }
      return JSON.parse(sessionStorage.getItem('testPaperInfo'))
    },

    getUserInfo (state) {
      if (state.userInfo.name) {
        return state.userInfo
      }
      return JSON.parse(localStorage.getItem('userInfo'))
    },

    getVideoUrl (state) {
      if (state.videoUrl) {
        return state.videoUrl
      }
      return JSON.parse(sessionStorage.getItem('videoUrl'))
    },

    getUserErrorQuestionList (state) {
      if (state.userErrorQuestionList.length > 0) {
        return state.userErrorQuestionList
      }
      return JSON.parse(sessionStorage.getItem('userErrorQuestionList'))
    }
  }
})
